
package com.enjoyiot.module.bms.controller.admin.device;

import com.enjoyiot.framework.common.enums.CommonStatusEnum;
import com.enjoyiot.framework.common.pojo.CommonResult;
import com.enjoyiot.framework.common.pojo.PageResult;
import com.enjoyiot.framework.common.util.object.BeanUtils;
import com.enjoyiot.module.bms.controller.admin.device.vo.*;
import com.enjoyiot.module.bms.dal.dataobject.BmsDeviceDO;
import com.enjoyiot.module.bms.sevice.device.BmsDeviceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import static com.enjoyiot.framework.common.pojo.CommonResult.success;

@Tag(name = "平台管理 - 设备信息")
@RestController
@RequestMapping("/bms/device")
@Validated
public class BmsDeviceController {

    @Resource
    private BmsDeviceService deviceService;

    @PostMapping("/create")
    @Operation(summary = "创建设备信息")
    @PreAuthorize("@ss.hasPermission('bms:device:create')")
    public CommonResult<Long> createDevice(@Valid @RequestBody BmsDeviceSaveReqVO createReqVO) {
        return success(deviceService.createDevice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备信息")
    @PreAuthorize("@ss.hasPermission('bms:device:update')")
    public CommonResult<Boolean> updateDevice(@Valid @RequestBody BmsDeviceSaveReqVO updateReqVO) {
        deviceService.updateDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bms:device:delete')")
    public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) {
        deviceService.deleteDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bms:device:query')")
    public CommonResult<BmsDeviceRespVO> getDevice(@RequestParam("id") Long id) {
        BmsDeviceDO device = deviceService.getDevice(id);
        return success(BeanUtils.toBean(device, BmsDeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备信息分页")
    @PreAuthorize("@ss.hasPermission('bms:device:query')")
    public CommonResult<PageResult<BmsDeviceRespVO>> getDevicePage(@Valid BmsDevicePageReqVO pageVO) {
        PageResult<BmsDevice> pageResult = deviceService.getDevicePage(pageVO);
        return success(BeanUtils.toBean(pageResult, BmsDeviceRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "设备状态修改")
    @PreAuthorize("@ss.hasPermission('bms:device:update')")
    public CommonResult<Boolean> updateDeviceStatus(@Valid @RequestBody BmsUpdateDeviceStatusReqVO reqVO) {
        deviceService.updateDeviceStatus(reqVO.getId(), reqVO.getDeviceStatus());
        return success(true);
    }

    @GetMapping("/sync-asset")
    @Operation(summary = "同步资产信息")
    @PreAuthorize("@ss.hasPermission('bms:device:sync')")
    public CommonResult<Integer> syncAsset() {
        return success(deviceService.syncAsset());
    }
}
